package com.zzh.phone.hviews;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.message.DialogMessageSettings;
import com.zzh.lib.core.listener.HOnClickListener;
import com.zzh.lib.core.utils.LogUtils;
import com.zzh.lib.views.HCountDownTextView;
import com.zzh.lib.views.HorizontalDownloadProgressBar;
import com.zzh.lib.views.def.PullToRefreshView;
import com.zzh.phone.hviews.databinding.ActivityMainBinding;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.ShareActionProvider;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class MainActivity extends AppCompatActivity implements Runnable {

    private ActivityMainBinding binding;
    Thread downLoadThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.lvv.setOnClickListener(new HOnClickListener() {
            @Override
            public void onSafeClick(View v) {
                LogUtils.d("-------------3333: Container");
            }

            @Override
            public void onIntervalClick(View v) {
                binding.lvv.showHeartAnim();
            }
        });
        binding.btnImageScale.setOnClickListener(new HOnClickListener() {
            @Override
            public void onSafeClick(View v) {
                startActivity(new Intent(MainActivity.this, com.zzh.phone.scale.MainActivity.class));
            }
        });
        binding.ptr.setMode(PullToRefreshView.Mode.PULL_BOTH);
        binding.ptr.setOnRefreshCallback(new PullToRefreshView.OnRefreshCallback() {
            @Override
            public void onRefreshingFromHeader(PullToRefreshView view) {
                binding.ptr.postDelayed(() -> {
                    runOnUiThread(() -> {
                        binding.ptr.stopRefreshingWithResult(true);
                    });
                }, 1000);
            }

            @Override
            public void onRefreshingFromFooter(PullToRefreshView view) {
                binding.ptr.postDelayed(() -> {
                    runOnUiThread(() -> {
                        binding.ptr.stopRefreshingWithResult(false);
                    });
                }, 1000);
            }
        });


        binding.btnVp2.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, VPAct.class)));
        binding.brb.setImageLoader(GlideEnginePhoto.getInstance());
        binding.btnDragGrid.setOnClickListener(v -> startActivity(new Intent(this, DragAct.class)));

        binding.brb.setSelectUrl("https://yx.bailingjk.net/icon/apply.png")
                .setUnSelectUrl("https://yx.bailingjk.net/icon/un-apply.png")
                .setUnSelectTextColor(Color.RED).setSelectTextColor(Color.CYAN).update();

        binding.btnHomeControl.setOnClickListener(v -> {
            binding.brb.setTextVisibility(binding.brb.getTextVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
        });
        downLoad();

        binding.hdp2.setOnClickListener(v -> {
            int progress = binding.hdp2.getProgress();
            int max = binding.hdp2.getMax();
            int state = binding.hdp2.getProgressState();
            if (progress == 0 && state == HorizontalDownloadProgressBar.START) {
                binding.hdp2.setProgressState(HorizontalDownloadProgressBar.DOWNLOADING);
                mIsDownload = true;
                mHandler.sendEmptyMessageDelayed(0x123, 400);
            } else if (progress >= 0 && progress < max && state == HorizontalDownloadProgressBar.DOWNLOADING) {
                binding.hdp2.setProgressState(HorizontalDownloadProgressBar.PAUSE);
                mIsDownload = false;
                mHandler.sendEmptyMessageDelayed(0x123, 400);
            } else if (progress >= 0 && progress < max && state == HorizontalDownloadProgressBar.PAUSE) {
                binding.hdp2.setProgressState(HorizontalDownloadProgressBar.DOWNLOADING);
                mIsDownload = true;
                mHandler.sendEmptyMessageDelayed(0x123, 400);
            }

        });

        binding.tvCountDown.setCountDownCallback(new HCountDownTextView.HCountDownCallback() {
            @Override
            public void onTick(HCountDownTextView textView, long millisUntilFinished) {
                Log.e("-----点击事件", "------------倒计时中------" + millisUntilFinished);
                textView.setText("剩余时间(" + millisUntilFinished / 1000 + ")");
            }

            @Override
            public void onFinish(HCountDownTextView textView) {
                Log.e("-----点击事件", "------------结束------");
                textView.setText("重新开始");
            }

            @Override
            public boolean onClick(HCountDownTextView textView) {
                Log.e("-----点击事件", "------点击------------");
                return true;
            }

            @Override
            public void onCancel(HCountDownTextView textView) {
                Log.e("-----点击事件", "-------------取消-----");
            }
        });

        binding.btn.setOnClickListener(v -> {
            binding.tvCountDown.cancelTimer();
        });
        binding.btnList.setOnClickListener(v -> {
           /* Intent intent = new Intent(MainActivity.this, ListActivity.class);

            startActivity(intent);*/
            binding.brb.setSelected(!binding.brb.isSelected());
        });
        binding.btnDrag.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, DragActivity.class);
            startActivity(intent);
        });

        binding.btnDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MaterialDialog dialog = new MaterialDialog(MainActivity.this, MaterialDialog.getDEFAULT_BEHAVIOR());
                dialog.message(0, "测试测试", new Function1<DialogMessageSettings, Unit>() {
                    @Override
                    public Unit invoke(DialogMessageSettings dialogMessageSettings) {
                        Log.e("-------", "-------");
                        return null;
                    }
                }).show();

                dialog.positiveButton(0, "确定", new Function1<MaterialDialog, Unit>() {
                    @Override
                    public Unit invoke(MaterialDialog materialDialog) {
                        Log.e("-------", "----确定---");
                        return null;
                    }
                }).negativeButton(0, "取消", new Function1<MaterialDialog, Unit>() {
                    @Override
                    public Unit invoke(MaterialDialog materialDialog) {
                        Log.e("-------", "----取消---");
                        return null;
                    }
                });
                dialog.show(new Function1<MaterialDialog, Unit>() {
                    @Override
                    public Unit invoke(MaterialDialog materialDialog) {
                        //materialDialog.message()
                        return null;
                    }
                });


            }
        });

        binding.btnListMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, MoreListViewActivity.class));
            }
        });

        binding.livReadMore.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, ReadMoreActivity.class)));

        binding.btnShare.setOnClickListener(v -> {
            ShareActionProvider sap = new ShareActionProvider(MainActivity.this);
            Intent intent = new Intent(Intent.ACTION_SEND);
            sap.setShareIntent(intent);

        });
    }

    private void downLoad() {
        downLoadThread = new Thread(this);
        downLoadThread.start();
    }

    @Override
    public void run() {

    }

    private int mProgress;
    private boolean mIsDownload = true;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x123) {
                if (mIsDownload) {
                    binding.hdp2.setProgress(mProgress++);
                    sendEmptyMessageDelayed(0x123, 40);
                }
            }
        }
    };
}